<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 发现特殊属性
 *
 * @author www.dingwei.cn
 */
class ConsumerFindAttributeModel extends MY_Model{

    public $tbAttribute = 'consumer_find_attribute';
    public $tbFindCategory = 'consumer_find_category';
    public $tbAttributeValue = 'consumer_find_attribute_value';
    public $tbAttributeCategory = 'consumer_find_attribute_category';

    /**
     * 获取列表数据
     * @param int $attributeCategoryId      特殊属性分类
     * @return array
     */
    public function adminPcGetList($attributeCategoryId = 0){
        if (!$attributeCategoryId){
            return array();
        }
        $this->db->where(array('attribute_category_id' => $attributeCategoryId))
            ->order_by('addtime desc');
        $data = $this->pagination($this->tbAttribute);

        return $data;
    }

    /**
     * 获取分类详情
     * @param $id
     * @return array
     * @author heyu
     */
    public function getInfo($id){
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));
        $info = $this->getOne($this->tbAttribute);
        if ($info && $info['attr_input_type'] == 3){
            $info['attr_values'] = json_decode($info['attr_values'], true);
        }
        return $info;
    }

    /**
     * 根据分类获取属性详情
     * @param $categoryId       int         分类id
     * @return array
     */
    public function getList($categoryId){
        if (!$categoryId){
            return array();
        }
        $this->db->select('a.id,a.attr_name,a.attr_input_type,a.attr_values')
            ->join($this->tbAttributeCategory.' fc', 'fc.id=a.attribute_category_id')
            ->where(array('fc.id' => $categoryId));
        $data = $this->getAll($this->tbAttribute.' a');

        if (!empty($data)){
            foreach ($data as $k => $v){
                if (isset($v['attr_values'])){
                    $attr_values = json_decode($v['attr_values'],JSON_UNESCAPED_UNICODE);
                }else{
                    $attr_values = array();
                }
                $data[$k]['attr_values'] = $attr_values;
            }
        }
        return $data;
    }

    /**
     * 保存数据
     * @param 	array 	$data
     * @param   integer $id
     * @return	boolean|integer
     * @author heyu
     */
    public function save($data, $id = 0){
        if($id){
            $this->db->where('id', $id);
            return $this->update($this->tbAttribute, $data);
        }

        return $this->insert($this->tbAttribute, $data);
    }

    /**
     * 批量保存数据
     * @param $data
     * @param $findId  有则删除曾经的。
     * @return bool
     */
    public function saveBatchAttribute($data,$findId = 0){
    	if ($findId){
    		//删除曾经的
    		$this->db->where('find_id',$findId);
    		if( ! $this->delete($this->tbAttributeValue)){
    			return false;
    		}
    	}
    	
        return $this->insertBatch($this->tbAttributeValue, $data);
    }

    /**
     * 获取属性的值
     * @param $findId
     * @return array
     */
    public function getAttributeValueList($findId){
        if (!$findId){
            return array();
        }
        $this->db->select('a.id,a.attribute_category_id,a.attr_name,a.attr_input_type,a.attr_values,v.attr_value,a.prefixes,a.suffix')
            ->join($this->tbAttribute.' a', 'a.id=v.attr_id')
            ->where(array('v.find_id' => $findId));
        $data = $this->getAll($this->tbAttributeValue.' v');
       
        get_instance()->loadModel('AdmSettingModel', 'mSetting', 'admin');
        if (!empty($data)) {
            foreach ($data as $k => $v) {
            	
            	if ($v['attr_input_type'] == 3){
                 	$attrValues = json_decode($v['attr_values'], true);
	                $data[$k]['attr_values'] = $attrValues;
            	}else{
            		$data[$k]['attr_values'] = array();
            	}
            	
            	$data[$k]['attr_value'] = $data[$k]['attr_value'];
            	$data[$k]['prefixes'] = empty($v['prefixes'])?"":$v['prefixes'];
            	$data[$k]['suffix'] = empty($v['suffix'])?"":$v['suffix'];
                if ($v['attr_input_type'] == 5) {
                    $data[$k]['attr_value']['city_list']  =  $this->mSetting->getLocationByPid($attrValue['province']);
                    $data[$k]['attr_value']['county_list'] = $this->mSetting->getLocationByPid($attrValue['city']);
                }
            }
        }
        return $data;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     * @author heyu
     */
    public function deleteAttribute($id){
        if (!$id){
            return false;
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbAttribute);
    }

    /**
     * 获取类型
     * @param null $k
     * @return array|mixed
     * @author heyu
     */
    public function getType($k = null){
        $config = array(
            '1' => '文本框输入(纯数字)',
            '2' => '文本框输入(文本)',
            '3' => '选择输入',
            '4' => '多行文本输入',
            '5' => '城市类'
        );
        if ($k === null){
            return $config;
        }
        return $config[$k];
    }

/************************特殊属性分类****************************/

    /**
     * 获取特殊分类列表数据
     * @return 	array
     * @author heyu
     */
    public function adminPcGetCategoryList(){

        $this->db->select('id,name')
            ->order_by('sort desc');
        $data = $this->getAll($this->tbAttributeCategory);

        return $data;
    }

    /**
     * 获取分类详情
     * @param $id
     * @return array
     * @author heyu
     */
    public function getCategoryInfo($id){
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));
        return $this->getOne($this->tbAttributeCategory);
    }

    /**
     * 获取最大排序号
     * @return int
     * @author heyu
     */
    public function getMaxSort(){
        $this->db->select('max(sort) as sort');
        $tA = $this->getOne($this->tbAttributeCategory);
        if (!$tA){
            return 1;
        }
        return $tA['sort']+1;
    }

    /**
     * 保存数据
     * @param 	array 	$data
     * @param   integer $id
     * @return	boolean|integer
     * @author heyu
     */
    public function saveCategory($data, $id = 0){
        if($id){
            $this->db->where('id', $id);
            return $this->update($this->tbAttributeCategory, $data);
        }

        return $this->insert($this->tbAttributeCategory, $data);
    }


    /**
     * 批量保存
     * @param $data
     * @param $key
     * @return bool
     */
    public function saveBatchCategory($data, $key){
        if ($key){
            return $this->updateBatch($this->tbAttributeCategory, $data, $key);
        }
        return $this->insertBatch($this->tbAttributeCategory, $data);
    }

    /**
     * 删除特殊分类
     * @param $id
     * @return bool
     */
    public function deleteAttributeCategory($id){
        if (!$id){
            return false;
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbAttributeCategory);
    }
}